/**
 * 
 */
package com.yidin.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;

/**
 * @author cola
 * 
 */
public class ImageAdapter {
	/**
	 * 加载本地图片
	 * 
	 * @param url
	 *            本地图片文件物理地址
	 * @return Bitmap
	 */
	public static Bitmap getLocalBitmap(String path, int width, int height) {
		try {
			FileInputStream fis = new FileInputStream(path);
			Bitmap bm = BitmapFactory.decodeStream(fis);
			fis.close();
			if (width <= 0 || height <= 0)
				return bm;

			int w = bm.getWidth();
			int h = bm.getHeight();
			// 计算缩放比例
			float scaleWidth = ((float) width) / w;
			float scaleHeight = ((float) height) / h;
			// 取得想要缩放的matrix参数
			Matrix matrix = new Matrix();
			matrix.postScale(scaleWidth, scaleHeight);
			// 得到新的图片
			Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, w, h, matrix, true);
			return newbm;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		} catch (Exception e) {
			return null;
		}
	}

	public static Bitmap getAssetsBitmap(Context ctx, String path, int width,
			int height) {
		try {
			InputStream in = ctx.getResources().getAssets().open(path);
			Bitmap bm = BitmapFactory.decodeStream(in);
			in.close();
			if (width <= 0 || height <= 0)
				return bm;

			int w = bm.getWidth();
			int h = bm.getHeight();
			// 计算缩放比例
			float scaleWidth = ((float) width) / w;
			float scaleHeight = ((float) height) / h;
			// 取得想要缩放的matrix参数
			Matrix matrix = new Matrix();
			matrix.postScale(scaleWidth, scaleHeight);
			// 得到新的图片
			Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, w, h, matrix, true);
			return newbm;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		} catch (Exception e) {
			return null;
		}
	}
}
